package cn.xiehangs;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Align;

public class 聊天框输入部分 {
    LazyBitmapFont font;
    int x,y,width,height;
    String name;
    TextField textField;
    Texture bgTexture;
    Texture cursorTexture;

    聊天框显示部分 聊天框显示部分_1;

    public 聊天框输入部分(String name,int x,int y,int width,int height,聊天框显示部分 聊天框显示部分_1){
        this.name = name;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.聊天框显示部分_1 = 聊天框显示部分_1;
        bgTexture = createBackgroundTexture();
        cursorTexture = createCursorTexture();
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.classpath("font.ttf"));
        Color color = new Color(0, 0, 0, 1);
        font = new LazyBitmapFont(generator, 20, color);
        TextField.TextFieldStyle style = new TextField.TextFieldStyle();
        style.font = font;
        style.fontColor = Color.BROWN;
        style.background = new TextureRegionDrawable(new TextureRegion(bgTexture));
        style.cursor = new TextureRegionDrawable(new TextureRegion(cursorTexture));
        textField = new TextField("聊天框……", style);
        textField.setPosition(x, y);
        textField.setSize(width, height);
        textField.setMaxLength(100);
        textField.setAlignment(Align.left);
        //添加点击事件
        textField.addListener(new com.badlogic.gdx.scenes.scene2d.utils.ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
               if (textField.getText().equals("聊天框……")) {
                   textField.setText("");
               }
            }
        });
        textField.addListener(new com.badlogic.gdx.scenes.scene2d.InputListener() {
            @Override
            public boolean keyDown(InputEvent event, int keycode) {
                if (keycode == Input.Keys.ENTER) {
                    String str = textField.getText();
                    textField.setText("");
                    聊天框显示部分_1.chat(name,str);
                }
                return false;
            }
        });
    }
    /**
     * 创建文本框的背景纹理
     */
    private Texture createBackgroundTexture() {
        Pixmap pixmap = new Pixmap(width,height, Pixmap.Format.RGBA8888);
        pixmap.setColor(1, 0, 0, 1);
        pixmap.drawRectangle(0, 0, pixmap.getWidth(), pixmap.getHeight());
        Texture texture = new Texture(pixmap);
        pixmap.dispose();
        return texture;
    }

    /**
     * 创建文本框中的光标纹理
     */
    private Texture createCursorTexture() {
        Pixmap pixmap = new Pixmap(1, height - 4, Pixmap.Format.RGBA8888);
        pixmap.setColor(1, 0, 0, 1);
        pixmap.fill();
        Texture texture = new Texture(pixmap);
        pixmap.dispose();
        return texture;
    }

}

